package com.vnas.database;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class FonanDatabase extends SQLiteOpenHelper {

	// path to database
	private static final String FONAN_DATABASE_PATH = "/data/data/com.vnas.fonan/databases/";

	// Name of database
	private static final String FONAN_DATABASE_NAME = "fonan";

	// database version

	private static final int FONAN_DATABASE_VERSION = 1;

	// tables

	public static final String FONAN_DATABASE_TABLE_ACCOUNT = "account";
	public static final String FONAN_DATABASE_TABLE_BALANCE = "balance";
	public static final String FONAN_DATABASE_TABLE_EXPENSES = "expenses";
	public static final String FONAN_DATABASE_TABLE_INCOME = "income";
	public static final String FONAN_DATABASE_TABLE_SAVING = "saving";

	private SQLiteDatabase db;
	private Context context;

	public FonanDatabase(Context context) {
		super(context, FONAN_DATABASE_NAME, null, FONAN_DATABASE_VERSION);

		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	// //////////////////////////////////////////////////////////////

	public void createDatabase() throws IOException {

		boolean isExist = isDatabaseExist();
		if (isExist)// db is exist.
		{
			// do nothing
		} else {
			try {
				//this.getReadableDatabase();
				copyDatabase();
			} catch (Exception e) {
				throw new IOException("Error while copying database.");
			}
		}
	}

	public boolean isDatabaseExist() {
		boolean re = false;
		SQLiteDatabase d = null;
		String dbp = FONAN_DATABASE_PATH + FONAN_DATABASE_NAME;
		try {
			d = SQLiteDatabase.openDatabase(dbp, null, 0);

		} catch (Exception e) {
			re = false;
		}
		if (d == null)// data has not been exist yet.
			re = false;
		else {
			re = true;
			d.close();
		}

		return re;
	}

	/**
	 * copy database from asset folder to internal folder
	 */

	public void copyDatabase() throws IOException {

		InputStream myInput = context.getAssets().open(FONAN_DATABASE_NAME);

		// Path to the our internal db
		String outFileName = FONAN_DATABASE_PATH + FONAN_DATABASE_NAME;

		OutputStream myOutput = new FileOutputStream(outFileName);

		// transfer bytes from db from asset folder to internal folder
		byte[] buffer = new byte[1024];
		int length;
		while ((length = myInput.read(buffer)) > 0) {
			myOutput.write(buffer, 0, length);
		}

		// Close the streams
		myOutput.flush();
		myOutput.close();
		myInput.close();
	}

	public void openDatabase() throws SQLException{
		String str = FONAN_DATABASE_PATH + FONAN_DATABASE_NAME;
		
		db = SQLiteDatabase.openDatabase(str, null, 0);
		
	}
	
	@Override
	public synchronized void close() {
		if(db!=null)
			db.close();
		super.close();
	}
	public SQLiteDatabase getSQLiteDatabase() throws SQLException
	{
		if(db==null)
			openDatabase();
		return db;
	}

}
